package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

/**
 * 其它交易控制类
 *
 * @author sunmi on 2017/2/12.
 */
public class TransactionOtherOperation {

    private static TransactionOtherOperation instance;
    private TransactionOtherSettings instanceSettings;

    private TransactionOtherOperation() {
        instanceSettings = getTransactionOtherSettings();
        if (instanceSettings == null) {
            instanceSettings = new TransactionOtherSettings();
            saveTransactionOtherSettings(instanceSettings);
        }
    }

    public static TransactionOtherOperation getInstance() {
        if (instance == null) {
            instance = new TransactionOtherOperation();
        }
        return instance;
    }

    public TransactionOtherSettings getTransactionOtherSettings() {
        if (instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(TransactionOtherSettings.TRANSACTION_OTHER_SETTINGS_KET);
            if (obj instanceof TransactionOtherSettings) {
                instanceSettings = (TransactionOtherSettings) obj;
            } else {
                instanceSettings = new TransactionOtherSettings();
                saveTransactionOtherSettings(instanceSettings);
            }
        }
        return instanceSettings;
    }

    public void init(boolean inputDirectorPsd,
                     boolean inputCardNum,
                     boolean defaultTransaction,
                     int maximumReturnAmount) {
        TransactionOtherSettings bean = getTransactionOtherSettings();
        bean.setInputDirectorPsd(inputDirectorPsd);
        bean.setInputCardNum(inputCardNum);
        bean.setDefaultTransaction(defaultTransaction);
        bean.setMaximumReturnAmount(maximumReturnAmount);
        saveTransactionOtherSettings(bean);
    }

    /**
     * 撤销退货类交易是否输入主管密码	1是0否	默认1
     */
    public void saveInputDirectorPsd(boolean inputDirectorPsd) {
        TransactionOtherSettings other = getTransactionOtherSettings();
        other.setInputDirectorPsd(inputDirectorPsd);
        saveTransactionOtherSettings(other);
    }

    /**
     * 电子签名
     *
     * @param supportESignature
     */
    public void saveSupportESignature(boolean supportESignature) {
        TransactionOtherSettings other = getTransactionOtherSettings();
        other.setSupportESignature(supportESignature);
        saveTransactionOtherSettings(other);
    }

    /**
     * 是否允许手输卡号	1是0否	默认0
     */
    public void saveInputCardNum(boolean inputCardNum) {
        TransactionOtherSettings other = getTransactionOtherSettings();
        other.setInputCardNum(inputCardNum);
        saveTransactionOtherSettings(other);
    }

    /**
     * 缺省交易选择	1消费0预授权	默认1
     */
    public void saveDefaultTransaction(boolean defaultTransaction) {
        TransactionOtherSettings other = getTransactionOtherSettings();
        other.setDefaultTransaction(defaultTransaction);
        saveTransactionOtherSettings(other);
    }

    /**
     * 最大退货金额	默认10000	可输入
     */
    public void saveMaximumReturnAmount(long maximumReturnAmount) {
        TransactionOtherSettings other = getTransactionOtherSettings();
        other.setMaximumReturnAmount(maximumReturnAmount);
        saveTransactionOtherSettings(other);
    }

    public void saveICCardShowCardNumber(boolean showICCardNumber) {
        TransactionOtherSettings other = getTransactionOtherSettings();
        other.setICCardShowCardNumber(showICCardNumber);
        saveTransactionOtherSettings(other);
    }

    public void saveTransactionOtherSettings(TransactionOtherSettings transactionOtherSettings) {
        this.instanceSettings = transactionOtherSettings;
        SharedPreferencesUtil.saveObj(transactionOtherSettings, TransactionOtherSettings.TRANSACTION_OTHER_SETTINGS_KET);
    }

    public void clearTransactionOtherSettings() {
        SharedPreferencesUtil.clearByKey(TransactionOtherSettings.TRANSACTION_OTHER_SETTINGS_KET);
    }


    //设置是否强制非接
    public void saveForceNFC(boolean isForceNFC) {
        TransactionOtherSettings other = getTransactionOtherSettings();
        other.setForceNFC(isForceNFC);
        saveTransactionOtherSettings(other);
    }


}
